{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-06T14:20:44.169709Z",
     "start_time": "2025-05-06T14:20:40.187668Z"
    }
   },
   "source": [
    "import torch\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T14:20:45.815365Z",
     "start_time": "2025-05-06T14:20:45.728853Z"
    }
   },
   "cell_type": "code",
   "source": [
    "torch.cuda.is_available()\n",
    "torch.cuda.device_count()"
   ],
   "id": "d865f771961e816d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:38.477821Z",
     "start_time": "2025-05-05T15:07:38.439308Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = np.arange(120).reshape(2,3,4,5)\n",
    "torch.tensor(data, dtype=None, device=None,requires_grad=False)"
   ],
   "id": "a205d5c81b933fc3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[  0,   1,   2,   3,   4],\n",
       "          [  5,   6,   7,   8,   9],\n",
       "          [ 10,  11,  12,  13,  14],\n",
       "          [ 15,  16,  17,  18,  19]],\n",
       "\n",
       "         [[ 20,  21,  22,  23,  24],\n",
       "          [ 25,  26,  27,  28,  29],\n",
       "          [ 30,  31,  32,  33,  34],\n",
       "          [ 35,  36,  37,  38,  39]],\n",
       "\n",
       "         [[ 40,  41,  42,  43,  44],\n",
       "          [ 45,  46,  47,  48,  49],\n",
       "          [ 50,  51,  52,  53,  54],\n",
       "          [ 55,  56,  57,  58,  59]]],\n",
       "\n",
       "\n",
       "        [[[ 60,  61,  62,  63,  64],\n",
       "          [ 65,  66,  67,  68,  69],\n",
       "          [ 70,  71,  72,  73,  74],\n",
       "          [ 75,  76,  77,  78,  79]],\n",
       "\n",
       "         [[ 80,  81,  82,  83,  84],\n",
       "          [ 85,  86,  87,  88,  89],\n",
       "          [ 90,  91,  92,  93,  94],\n",
       "          [ 95,  96,  97,  98,  99]],\n",
       "\n",
       "         [[100, 101, 102, 103, 104],\n",
       "          [105, 106, 107, 108, 109],\n",
       "          [110, 111, 112, 113, 114],\n",
       "          [115, 116, 117, 118, 119]]]], dtype=torch.int32)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "把requires_grad设置成true或false要灵活处理。如果是训练过程就要设置为true，⽬的是⽅便求导、更新参数。⽽到了验证或者测试过程，我们的⽬的是检查前模型的泛化能⼒，那就要把 requires_grad 设置成 Fasle，避免这个参数根据 loss ⾃动更新。",
   "id": "b08cfd8b9cbc4dbf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:38.629407Z",
     "start_time": "2025-05-05T15:07:38.607141Z"
    }
   },
   "cell_type": "code",
   "source": [
    "torch.from_numpy(data) # 直接从numpy中创建\n",
    "# torch.zeros(5, dtype=None) # 0矩阵\n",
    "# torch.ones(10, dtype=None) # 1矩阵\n",
    "# torch.eye(8, dtype=None) #对角线矩阵\n",
    "# torch.rand(10, dtype=None) #浮点矩阵\n",
    "# torch.randn(10, dtype=None) #0-1方差矩阵\n",
    "#torch.normal(2, 3, 5) #浮点矩阵\n",
    "# torch.randint(2, 3, 5) #随机整数矩阵"
   ],
   "id": "95fd113098ba6c0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[  0,   1,   2,   3,   4],\n",
       "          [  5,   6,   7,   8,   9],\n",
       "          [ 10,  11,  12,  13,  14],\n",
       "          [ 15,  16,  17,  18,  19]],\n",
       "\n",
       "         [[ 20,  21,  22,  23,  24],\n",
       "          [ 25,  26,  27,  28,  29],\n",
       "          [ 30,  31,  32,  33,  34],\n",
       "          [ 35,  36,  37,  38,  39]],\n",
       "\n",
       "         [[ 40,  41,  42,  43,  44],\n",
       "          [ 45,  46,  47,  48,  49],\n",
       "          [ 50,  51,  52,  53,  54],\n",
       "          [ 55,  56,  57,  58,  59]]],\n",
       "\n",
       "\n",
       "        [[[ 60,  61,  62,  63,  64],\n",
       "          [ 65,  66,  67,  68,  69],\n",
       "          [ 70,  71,  72,  73,  74],\n",
       "          [ 75,  76,  77,  78,  79]],\n",
       "\n",
       "         [[ 80,  81,  82,  83,  84],\n",
       "          [ 85,  86,  87,  88,  89],\n",
       "          [ 90,  91,  92,  93,  94],\n",
       "          [ 95,  96,  97,  98,  99]],\n",
       "\n",
       "         [[100, 101, 102, 103, 104],\n",
       "          [105, 106, 107, 108, 109],\n",
       "          [110, 111, 112, 113, 114],\n",
       "          [115, 116, 117, 118, 119]]]], dtype=torch.int32)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:38.720928Z",
     "start_time": "2025-05-05T15:07:38.697928Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# a = torch.tensor(1) #int与tensor转换\n",
    "# b = a.item()\n",
    "\n",
    "a1 = [1,2,3]\n",
    "b1 = torch.tensor(a1)\n",
    "c = b1.numpy().tolist()"
   ],
   "id": "1536e9efd545cb5a",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:51.928994Z",
     "start_time": "2025-05-05T15:07:51.895998Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data.cuda() # CPU -> GPU\n",
    "data.cpu() # GPU -> CPU"
   ],
   "id": "751df0e52c442e1c",
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object has no attribute 'cuda'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[9], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mdata\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcuda\u001B[49m() \u001B[38;5;66;03m# CPU -> GPU\u001B[39;00m\n\u001B[0;32m      2\u001B[0m data\u001B[38;5;241m.\u001B[39mcpu() \u001B[38;5;66;03m# GPU -> CPU\u001B[39;00m\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'numpy.ndarray' object has no attribute 'cuda'"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:56.304264Z",
     "start_time": "2025-05-05T15:07:56.278267Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.rand(2,3,5)\n",
    "print(x.shape)\n",
    "x = x.permute(1,2,0) #维度转换\n",
    "print(x.shape)\n",
    "x = x.transpose(1,0) #维度调换\n",
    "print(x.shape)"
   ],
   "id": "841945678e4ef561",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3, 5])\n",
      "torch.Size([3, 5, 2])\n",
      "torch.Size([5, 3, 2])\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "注意此时转换后内存并不连贯",
   "id": "bbee19bdd2eed407"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:38.863968300Z",
     "start_time": "2025-04-30T17:47:02.450083Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# x.view(1,2,15) # 此时会报错\n",
    "x.reshape(1,6,5) #使用reshape避免报错"
   ],
   "id": "ff0c5166b9a7e93",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.0228, 0.3594, 0.7902, 0.3401, 0.9164],\n",
       "         [0.4382, 0.2343, 0.6545, 0.8724, 0.0314],\n",
       "         [0.3967, 0.5286, 0.2061, 0.6129, 0.3132],\n",
       "         [0.4091, 0.6986, 0.3975, 0.3989, 0.0811],\n",
       "         [0.3564, 0.8291, 0.2756, 0.0582, 0.9497],\n",
       "         [0.4537, 0.0733, 0.7833, 0.2213, 0.9904]]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:07:38.864971400Z",
     "start_time": "2025-04-30T17:49:56.976595Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x.squeeze(0) #删减维度\n",
    "x.unsqueeze(2) #在第二个维度插入一个新维度"
   ],
   "id": "34993b848f7e8bb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[0.0228, 0.3594]],\n",
       "\n",
       "         [[0.7902, 0.3401]],\n",
       "\n",
       "         [[0.9164, 0.4382]]],\n",
       "\n",
       "\n",
       "        [[[0.2343, 0.6545]],\n",
       "\n",
       "         [[0.8724, 0.0314]],\n",
       "\n",
       "         [[0.3967, 0.5286]]],\n",
       "\n",
       "\n",
       "        [[[0.2061, 0.6129]],\n",
       "\n",
       "         [[0.3132, 0.4091]],\n",
       "\n",
       "         [[0.6986, 0.3975]]],\n",
       "\n",
       "\n",
       "        [[[0.3989, 0.0811]],\n",
       "\n",
       "         [[0.3564, 0.8291]],\n",
       "\n",
       "         [[0.2756, 0.0582]]],\n",
       "\n",
       "\n",
       "        [[[0.9497, 0.4537]],\n",
       "\n",
       "         [[0.0733, 0.7833]],\n",
       "\n",
       "         [[0.2213, 0.9904]]]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
